<template>
	<el-dialog v-model="dialogVisible" title="发货" width="60%" :before-close="handleClose" :destroy-on-close="true">
		<div style="font-size: 14px; line-height: 30px">
			<el-descriptions title="商品信息" direction="vertical" size="small" :column="2" border>
				<el-descriptions-item label="商品图片">
					<el-image :src="props.row.detailVO.skuImage" class="image" :preview-src-list="[props.row.detailVO.skuImage]"></el-image>
				</el-descriptions-item>
				<el-descriptions-item label="商品名称">{{ props.row.detailVO.goodsName }}</el-descriptions-item>
				<el-descriptions-item label="sku名称">{{ props.row.detailVO.skuName }}</el-descriptions-item>
				<el-descriptions-item label="购买数量">{{ props.row.detailVO.skuNum }}</el-descriptions-item>
				<el-descriptions-item label="购买时价格">{{ props.row.detailVO.skuPresentPrice }}</el-descriptions-item>
			</el-descriptions>

			<el-descriptions title="订单信息" direction="vertical" size="small" :column="2" border>
				<el-descriptions-item label="订单号">{{ props.row.orderNo }}</el-descriptions-item>
				<el-descriptions-item label="下单时间">{{ props.row.orderTime }}</el-descriptions-item>
				<el-descriptions-item label="订单总金额">{{ props.row.goodsAmount }}</el-descriptions-item>
				<el-descriptions-item label="商家备注">{{ props.row.remark }}</el-descriptions-item>
			</el-descriptions>

			<el-descriptions title="收货信息" direction="vertical" size="small" :column="2" border style="margin-top: 20px">
				<el-descriptions-item label="收货人">{{ props.row.consigneeName }}</el-descriptions-item>
				<el-descriptions-item label="联系方式">{{ props.row.consigneePhone }}</el-descriptions-item>
				<el-descriptions-item label="买家备注" span="2">{{ props.row.consigneeaRemark ? props.row.consigneeaRemark : '-' }}</el-descriptions-item>
				<el-descriptions-item label="地址" span="2">{{ props.row.consigneeAddress }}</el-descriptions-item>
			</el-descriptions>

			<el-form :model="form" label-width="120px" :rules="rules" ref="formRef" style="margin-top: 30px">
				<el-form-item label="快递公司：" prop="expressCode">
					<el-select v-model="form.expressCode" filterable placeholder="请选择快递公司" style="width: 250px" @change="expressChange">
						<el-option v-for="(item, index) in logisticsList" :label="item.delivery_name" :value="item.delivery_id" :key="item.id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="单号：" prop="expressNo">
					<el-input v-model="form.expressNo" placeholder="请输入单号" style="width: 250px"></el-input>
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="submit">提交</el-button>
				<el-button @click="handleClose">关闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus';
import { ref, watch, onBeforeMount, reactive } from 'vue';
import { doOrderSend, getLogisticsList } from '/@/api/api';

const form = ref({});
const formRef = ref();

const loading = ref(false);
const props = defineProps({
	// 列表内容
	dialogVisible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => {},
	},
});

const rules = reactive({
	name: [{ required: true, message: '请输入商品名字', trigger: 'blur' }],
	categoryId: [{ required: true, message: '请选择商品类目', trigger: 'blur' }],
});

const dialogVisible = ref(false);

watch(
	() => props.dialogVisible,
	async (val) => {
		dialogVisible.value = val;
		if (val) {
			form.value.id = props.row.id;
			getLogisticsListFn();
		}
	}
);

onBeforeMount(() => {});

function expressChange(code) {
	logisticsList.value.forEach((item, index) => {
		if (item.delivery_id == code) {
			form.value.expressName = item.delivery_name;
		}
	});
}
const logisticsList = ref([]);
async function getLogisticsListFn() {
	const { code, data } = await getLogisticsList({});
	if (code === 200) {
		logisticsList.value = data.list;
	}
}

// 发货
async function submit() {
	let par = {
		...form.value,
	};
	const { code } = await doOrderSend(par);
	if (code === 200) {
		ElMessage.success('发货成功');
		handleClose();
	}
}

const emit = defineEmits(['handleClose']);

function handleClose() {
	form.value = {};
	formRef.value.resetFields();
	emit('handleClose');
}

function labelHandleClose() {
	selectLabelVisible.value = false;
}
</script>

<style>
.desc_label {
	width: 20px;
}

.image {
	width: 70px;
	height: 70px;
}
</style>
